/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * $Id: EnvironmentMeasurementClient.hpp,v 1.2 2009-05-13 07:18:16 amaula Exp $
 *
 * \file
 * \brief MaCI - EnvironmentMeasurement Interface Client header
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 */
#ifndef _MACI_INTERFACE_ENVIRONMENTMEASUREMENTCLIENT_HPP_
#define _MACI_INTERFACE_ENVIRONMENTMEASUREMENTCLIENT_HPP_

#include "MaCI.hpp"
#include "thread.hpp"
#include "sync.hpp"

// These are not necessarily required here, but defined to make the
// interface easier to include.
#include "EnvironmentMeasurementData.hpp"

// Forward declare gimi::GIMI
namespace gimi {
  class GIMI;
}

namespace MaCI {
  namespace EnvironmentMeasurement {
    
    // Forward declaration.
    class CEnvironmentMeasurementData;
      
    /** MaCI - EnvironmentMeasurement Client interface.
     * 
     * This class contains the methods and types for using
     * a MaCI::EnvironmentMeasurement service in the GIMnet.
     */
    class CEnvironmentMeasurementClient 
      : private gim::CSync,
        private gim::CThread,
        public MaCI::CMaCI
    {
    public:
      /** Constructor.
       * 
       * Constructor of the EnvironmentMeasurementClient. This requires;
       * a pointer to Initialized and connected GIMI, minor
       * number of this interface (Used as GIMI minor number)
       *
       * @param[in] aGIMIPtr Pointer to GIMI used for communication
       * @param[in] aInterfaceMinor Interface minor number. Usually
       *            used to indicate the instance of devices of same
       *            type. Can be left out if MaCISL is used.
       */     
      CEnvironmentMeasurementClient(gimi::GIMI *aGIMIPtr, const int aInterfaceMinor = -1);

      
      /** Destructor.
       */
      ~CEnvironmentMeasurementClient();


            
      /**
       * Sends environment measurement to server, and waits for reply for aTimeout_ms ms.
       *
       * @param[in] aEnvData        Environment measurement data to send
       * @param[out] aReplyData     Reply data, which contains at least position and variance
       * @param[in] aTimeout_ms     Timeout value. If the operation is not completed
       *                            before timeout is reached, the operation fails.
       *
       * @return                    true - Operation was succesfull, new data is stored
       *                            on provided locations.
       *                            false - Operation failed; timeout or generic error
       *                            encountered.
       */
      bool GetPositionWithEnvironment(CEnvironmentMeasurement2D &aEnvData, 
                                      CEnvironmentMeasurementData *aReplyData, 
                                      const int aTimeout_ms );
    protected:
   
       /** Send EnvironmentMeasurement Command.
       *
       * Send the given EnvironmentMeasurement Data element to server side of the 
       * EnvironmentMeasurement interface.
       *
       *
       * @param[in] aEnvData    Reference to element to send.
       * @param[in] aMessageID  MessageID to set for sent command.
       * @return                'true' if Message was succesfully sent to GIMnet. (No guarantee
       *                        of delivery is given though). 'false' on local connection
       *                        error or invalid datapacket (the container didn't contain
       *                        a TCommand element, which renders this command meaningless)
       */
      bool SendCommand(const CEnvironmentMeasurementData &aEnvData, const int aMessageID);
   

      /** Open client interface.
       *
       * Implementation of Open() for the class. This function is
       * called by the base class CMaCI::Open() function.
       */
      EMaCIError DoOpen(void);
                                      
      /** Close client interface.
       *
       * Implementation of Close() for the class. This function is
       * called by the base class CMaCI::Close() function.
       */
      EMaCIError DoClose(void);

      /** Receive reply for a specific command.
       *
       * This function attempts to reveive a reply from replyQueue.
       *
       *
       * @param[out] aData      Reference to element to send.
       * @param[in] aMessageID  MessageID to set for sent command.
       * @param[in] aTimeout_ms Timeout in milliseconds to wait for message
       *                        to arrive.
       * @return                'true' if reply with given id was succesfully received.
       *                        'false' on any error (including Timeout)
       */
      bool GetCommandReply(CEnvironmentMeasurementData &aData, 
                           const int aMessageID, 
                           const int aTimeout_ms);

    private:
      /// Thread runner function. Is in charge of receiving events.
      int ThreadFunction(const int aThreadNumber);
      int ProcessMessage(const gimi::GIMIMessage &msg);

      int iEnvironmentMeasurementCommandSequence;

      // Override copy & assignment operator privately to disable class copying.
      CEnvironmentMeasurementClient(const CEnvironmentMeasurementClient &)
        : CSync(1,1),
          CThread(1),
          CMaCI(NULL, 0, 0, ""),
          iEnvironmentMeasurementCommandSequence(){}

      CEnvironmentMeasurementClient &operator=(const CEnvironmentMeasurementClient &) { return *this; }
    };


  }
}

#endif //_MACI_INTERFACE_ENVIRONMENTMEASUREMENTCLIENT_HPP_
